#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    fd_set readfds;
    FD_ZERO(&readfds);  // 清空集合
    FD_SET(STDIN_FILENO, &readfds);  // 添加标准输入
    
    struct timeval timeout;
    timeout.tv_sec = 5;  // 5秒超时
    timeout.tv_usec = 0;
    
    printf("等待输入(5秒超时)...\n");
    int ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout);
    
    if (ret == -1) {
        perror("select error");
    } else if (ret) {
        if (FD_ISSET(STDIN_FILENO, &readfds)) {
            printf("数据可读\n");
        }
    } else {
        printf("超时\n");
    }
    
    return 0;
}